
requirejs(['jquery', 'Util', 'bootstrap', 'nesTable'], function ($, Util) {
    console.log('admin-cat-config');

    render_catTree();

    // 新增分类按钮点击
    $('#cat-add-btn').on('click', function () {
        var p_id = $('#p-cat').val();
        var cat_name = $('#cat-name').val();
        if (!cat_name)
            $('#cat-name').addClass('error');

        $.post('/admin/ajax_set_cat_config', {
            p_id: p_id,
            cat_name: cat_name
        }, function (ret) {
            if (ret) {
                Util.Alert('新增成功', 1);
                $('#cat-name').val('');
                render_catTree();
            }
        });
    });

    // 渲染分类树
    function render_catTree() {
        $.get('/admin/ajax_load_catTree', function (ret) {
            $(ret).appendTo($('#nestable'));
            $('#nestable').nestable();
            $('.dd').nestable('collapseAll');
        });
    }

    //展开,收起按钮点击
    $('#nestable-menu').on('click', function (e) {
        var target = $(e.target);
        var action = target.data('action');
        if (action === 'expand-all') {
            $('.dd').nestable('expandAll');
        }
        if (action === 'collapse-all') {
            $('.dd').nestable('collapseAll');
        }
    });

});


